<template>
  <div class="container">
    <Save-Announcement ref="saveAnnouncementRef" @refresh="search" />
    <a-card class="general-card" :title="$route.meta.label">
      <a-row style="margin-bottom: 16px">
        <a-col :span="16">
          <a-space>
            <a-button type="primary" @click="handleClickToSave()">
              <template #icon>
                <icon-plus />
              </template>
              {{ $t('searchTable.operation.create') }}
            </a-button>
          </a-space>
        </a-col>
      </a-row>
      <a-table
        row-key="id"
        :loading="loading"
        :data="renderData"
        column-resizable
        :hide-expand-button-on-empty="true"
        :bordered="false"
        :pagination="{
          current: pagination.page,
          total: pagination.total,
          pageSize: pagination.rows,
          showTotal: true,
          showPageSize: true,
        }"
        @page-change="onPageChange"
        @page-size-change="onPageSizeChange"
      >
        <template #columns>
          <a-table-column title="标题" data-index="title" :width="150" />
          <a-table-column title="内容" data-index="text" :width="200" />
          <a-table-column
            title="创建人"
            data-index="creatorName"
            align="right"
            :width="150"
          />
          <a-table-column
            title="创建时间"
            data-index="createTime"
            align="right"
            :width="200"
          >
          </a-table-column>
          <a-table-column
            :title="$t('columns.operations')"
            align="right"
            data-index="operations"
            :width="260"
          >
            <template #cell="{ record }">
              <a-button
                type="text"
                size="small"
                @click="handleClickToShow(record.id)"
              >
                {{ record.showFlag == 0 ? '发布' : '取消发布' }}
              </a-button>
              <a-button
                type="text"
                size="small"
                @click="handleClickToEdit(record)"
              >
                {{ $t('columns.operations.edit') }} </a-button
              ><a-popconfirm
                :content="$t('operations.popConfirm.delete')"
                type="error"
                @ok="handleClickToDel(record.id)"
              >
                <a-button type="text" size="small" status="danger">
                  {{ $t('columns.operations.delete') }}
                </a-button>
              </a-popconfirm>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import useLoading from '@/hooks/loading';
  import { Message } from '@arco-design/web-vue';
  import {
    AnnouncementVO,
    AnnouncementParams,
    getAnnouncementList,
    showSystemAnnouncement,
    deleteAnnouncement,
  } from '@/api/announcement-management';
  import SaveAnnouncement from './components/save-announcement.vue';

  const { loading, setLoading } = useLoading(false);
  const renderData = ref();
  const saveAnnouncementRef = ref();
  const pagination = reactive({
    page: 1,
    rows: 20,
    total: 0,
  });
  const fetchData = async (params: AnnouncementParams = pagination) => {
    setLoading(true);
    try {
      const { data } = await getAnnouncementList(params);
      renderData.value = data.rows;
      pagination.total = data.total;
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };

  const search = () => {
    fetchData();
  };
  const onPageChange = (current: number) => {
    pagination.page = current;
    fetchData();
  };
  const onPageSizeChange = (current: number) => {
    pagination.rows = current;
    fetchData();
  };

  const handleClickToSave = () => {
    saveAnnouncementRef.value.handleChangeVisible();
  };

  const handleClickToEdit = (obj: AnnouncementVO) => {
    saveAnnouncementRef.value.handleChangeVisible(obj);
  };

  const handleClickToShow = (id: string) => {
    showSystemAnnouncement(id).then(() => {
      Message.success('操作成功');
      search();
    });
  };
  const handleClickToDel = (id: string) => {
    deleteAnnouncement(id).then(() => {
      Message.success('操作成功');
      search();
    });
  };
  search();
</script>

<style scoped lang="less">
  .container {
    padding: 10px;
  }

  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
  :deep(.arco-table-td-content) {
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    display: -webkit-box !important;
    -webkit-line-clamp: 3 !important;
    -webkit-box-orient: vertical !important;
  }
</style>
